//
//  PKNewFeatureView.swift
//  Panku
//
//  Created by mac on 2017/2/7.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit

let PKNewFeatureViewCellID = "PKNewFeatureViewCellID"

class PKNewFeatureView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate{
    
    static let sharedInstance = PKNewFeatureView()
    
    //MARK: -Properties
    //图片数组
    let images = [R.image.newfeature_01(),R.image.newfeature_02(),R.image.newfeature_03()]
    
    var layout:UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: APP_WIDTH, height: APP_HEIGHT)
        return layout
    }()

    private init() {
        super.init(frame: CGRect.zero, collectionViewLayout: layout)
        
        self.dataSource = self
        self.delegate = self
        
        //page_enable
        self.isPagingEnabled = true
        
        self.register(PKNewFeatureViewCell.self, forCellWithReuseIdentifier: PKNewFeatureViewCellID)
        
        self.backgroundColor = UIColor.black
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //show类方法
    class func show(In view:UIView = KEY_WINDOW!) -> Void{
        
        let sharedInstance = PKNewFeatureView.sharedInstance
        sharedInstance.alpha = 1
        sharedInstance.transform = .identity
        
        view.addSubview(sharedInstance)
        view.bringSubview(toFront: sharedInstance)
        
        sharedInstance.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
    }
    
    //dismiss类方法
    class func dismiss() -> Void{
        
        let sharedInstance = PKNewFeatureView.sharedInstance

        UIView.animate(withDuration: TIME_INTERVAL_DEFAULT, animations: { 
            sharedInstance.alpha = 0
            sharedInstance.transform = CGAffineTransform(scaleX: 2, y: 2)
        }) { (_) in
            sharedInstance.removeFromSuperview()
        }
    }
    

}

//PKNewFeatureView dataSource
extension PKNewFeatureView{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PKNewFeatureViewCellID, for: indexPath)
        return cell
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        //初始化cell
        (cell as! PKNewFeatureViewCell).imageView.image = images[indexPath.item]
        
        if indexPath.item == images.count - 1{
            (cell as! PKNewFeatureViewCell).enterButton.isHidden = false
        }else{
            (cell as! PKNewFeatureViewCell).enterButton.isHidden = true
        }
        
    }
    
}
